package com.spbdx.common.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.symmetric.SM4;

/**
 * sm2,sm3,sm4工具类
 */
public class SmUtils {

    private static SM4 sm4 = SmUtil.sm4(Base64.decode("JfCQkDzK+b7671KlMDo5BQ=="));

    private static SM2 sm2 = SmUtil.sm2("4cnDvJbEOY1x2RSOhJzDbo0vWpvooQZ2ZtrCD/VrllE=", "A6kbKedgr6I8GVXPx+9WldLT7B1MCbMUV2rKYYDE23KX");

    /**
     * sm4加密
     *
     * @param str
     * @return
     */
    public static String sm4Encrypt(String str) {
        String encryptHex = sm4.encryptHex(str);
        return encryptHex;

    }

    /**
     * sm4解密
     *
     * @param str
     * @return
     */
    public static String sm4Decrypt(String str) {
        return sm4.decryptStr(str, CharsetUtil.CHARSET_UTF_8);
    }


    public static String sm2Encrypt(String str) {
        return sm2.encryptBcd(str, KeyType.PublicKey);
    }

    public static String sm2Decrypt(String str) {
        return sm2.decryptStr(str, KeyType.PrivateKey);
    }

    public static void main(String[] args) {
        String s = "test中文frfewrewrwerwer---------------------------------------------------";
        String encryptHex = sm2Encrypt(s);
        System.out.println(encryptHex);
        String decrypt = sm2Decrypt(encryptHex);
        System.out.println(decrypt);
    }
}
